//
//  ExpenseController.m
//  MoneyIndividual
//
//  Created by Tai Le on 12/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "ExpenseController.h"
#import "TitleTextFieldCell.h"
#import "TitleNumberCell.h"
#import "TitleExpenseTypeCell.h"
#import "TitleCatalogCell.h"
#import "TitleDateCell.h"
#import "TitleRepeatCell.h"
#import "TitleTextViewCell.h"

@interface ExpenseController ()

@end

@implementation ExpenseController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"Chi";
    
    _accountCell = [self newCellWithTitle:NSLocalizedString(@"Loại", nil) placeHolder:@"" class:[TitleExpenseTypeCell class] key:ACCOUNT_ENITY];
    
    [self generateRowsForType:TransactionTypeExpensePermanent managedObject:[[CoreData shared] newEntity:TRANSACTION_ENTITY]];
}

#pragma mark - Logic functions
- (void)generateRowsForType:(TransactionType)type managedObject:(NSManagedObject*)managedObject
{
    [_rows removeAllObjects];
    self.managedObject = managedObject;
    
    _accountCell.selectedValue = [[CoreData shared] getAccountByType:type];
    
    if (type == TransactionTypeExpensePermanent)
    {
        [self generateRowsForExpensePermanent];
    }
    else if (type == TransactionTypeExpenseNonpermanent)
    {
        [self generateRowsForExpenseNonpermanent];
    }
    else if (type == TransactionTypeExpenseLoan)
    {
        [self generateRowsForExpenseLoan];
    }
    else if (type == TransactionTypeExpenseDebt)
    {
        [self generateRowsForExpenseDebt];
    }
    else if (type == TransactionTypeExpenseHui)
    {
        [self generateRowsForExpenseHui];
    }
    
    [self loadCatalogDataByAccount];
    
    [self.tableView reloadData];
}

- (void)generateRowsForExpensePermanent
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Chi cho", nil)
                                placeHolder:NSLocalizedString(@"Chọn mục chi", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForExpenseNonpermanent
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Chi cho", nil)
                                placeHolder:NSLocalizedString(@"Chọn mục chi", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForExpenseLoan
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Cho", nil)
                                placeHolder:NSLocalizedString(@"ai", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Vay số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForExpenseDebt
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Trả cho", nil)
                                placeHolder:NSLocalizedString(@"ai", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Nợ gốc trả", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForExpenseHui
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Dây hụi", nil)
                                placeHolder:NSLocalizedString(@"Chọn dây hụi", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

@end
